package com.xiaobai.service.achievement;

import com.alibaba.fastjson.JSON;
import com.xiaobai.dao.AchievementDao;
import com.xiaobai.pojo.Achievement;
import com.xiaobai.service.UnitAchievementService;
import com.xiaobai.units.HttpUnits;
import com.xiaobai.vo.AchievementItem;
import com.xiaobai.vo.AllAchievementItemAllList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class AchievementServiceImpl implements IAchievementService {

    @Autowired
    private UnitAchievementService unitAchievementService;

    @Autowired
    private AchievementDao achievementDao;

    @Override
    public AllAchievementItemAllList firstSelectAchievement(String jsessionid,String student_id) {

        /**
         * 一次http查询出所有的,避免第二学期会到新的一年,所以还是不根据当前年份制定循环了
         */

        AllAchievementItemAllList allAchievementItemAllList=new AllAchievementItemAllList();

        //先查出公选课的成绩
        String gxk=selectAllAchievement(jsessionid,"02");
//        String gxk="{\"total\":0,\"rows\":[]}";

        /**
         * 注意公选课为空的情况,为空就不要截取了(为空就算字符串长度很短!!!)
         */
        if(gxk.length()<30){

            //没有公选课成绩,就不管,因为构造函数已经创建了一个空的ZeroList

        }else{
            //有公选课成绩
            int startIndex=gxk.indexOf("\"rows\":")+7;
            int endIndex=gxk.length()-1;//正确

            gxk=gxk.substring(startIndex,endIndex);
            List<String> gxkJSONList = JSON.parseArray(gxk, String.class);

            List<AchievementItem> gxkItemList=new ArrayList<>();

            for (int i=0;i<gxkJSONList.size();i++){
                AchievementItem achievementItem=
                        JSON.parseObject(gxkJSONList.get(i),AchievementItem.class);

                gxkItemList.add(achievementItem);
            }
            allAchievementItemAllList.setZeroAchievementList(gxkItemList);
        }

        /**
         * 也要注意新生没成绩的情况~
         */


        String result=selectAllAchievement(jsessionid,"01");
//        String result="{\"total\":0,\"rows\":[]}";


        if(result.length()<30){

            //小萌新 没成绩 直接存入
            unitAchievementService.saveAchievement(student_id,allAchievementItemAllList);

            return allAchievementItemAllList;

        }else {

            int start=0;//防止重复的黄色下划线

            start=result.indexOf("\"rows\":")+7;

            int end=result.length()-1;//正确

            result=result.substring(start,end);

            List<String> pyjhList = JSON.parseArray(result, String.class);

            List<AchievementItem> pyjhItemList=new ArrayList<>();

            for(int i=0;i<pyjhList.size();i++){
                //都加进itemList
                pyjhItemList.add(JSON.parseObject(pyjhList.get(i),AchievementItem.class));
            }

            Integer gender=Integer.parseInt(student_id.substring(0,2));

            for(int i=0;i<pyjhItemList.size();i++){
                //正式遍历

                /**
                 * 如何算出目前是第几学期呢?很简单哦,用xnxqmc
                 * 1.截取3,4位得出考试的年份 再截取最后一位得出上下学期
                 * 2.根据以上数据和学号前两位即可得出
                 */

                AchievementItem achievementItem=pyjhItemList.get(i);

                Integer ksyear=Integer.parseInt(achievementItem.getXnxqmc().substring(2,4));
                Integer ksxq=Integer.parseInt(achievementItem.getXnxqmc().substring(10,11));
                Integer xq=(ksyear-gender)*2+ksxq;

                switch (xq){
                    case 1:
                        allAchievementItemAllList.getOneAchievementList().add(achievementItem);
                        break;
                    case 2:
                        allAchievementItemAllList.getTwoAchievementList().add(achievementItem);
                        break;
                    case 3:
                        allAchievementItemAllList.getThreeAchievementList().add(achievementItem);
                        break;
                    case 4:
                        allAchievementItemAllList.getFourAchievementList().add(achievementItem);
                        break;
                    case 5:
                        allAchievementItemAllList.getFiveAchievementList().add(achievementItem);
                        break;
                    case 6:
                        allAchievementItemAllList.getSixAchievementList().add(achievementItem);
                        break;
                    case 7:
                        allAchievementItemAllList.getSevenAchievementList().add(achievementItem);
                        break;
                    case 8:
                        allAchievementItemAllList.getEightAchievementList().add(achievementItem);
                        break;
                }
            }
        }

        /**
         * 最后是异步存入数据库
         */

        unitAchievementService.saveAchievement(student_id,allAchievementItemAllList);

        return allAchievementItemAllList;
    }

    @Override
    public void updateOrInsertAchievement(String student_id, String term,String upContent) {
        achievementDao.updateOrInsertAchievement(student_id,term,upContent);
    }

    @Override
    public AllAchievementItemAllList selectAchievementByDB(String student_id) {

        AllAchievementItemAllList allAchievementItemAllList=new AllAchievementItemAllList();

        /**
         * 批量查出八个学期的
         */
        List<Achievement> list=achievementDao.selectAchievementByDB(student_id);

        for(int i=0;i<=8;i++){
            switch (i){
                case 0:
                    List<String> list0=JSON.parseArray(list.get(0).getContent(),String.class);
                    List<AchievementItem> itemList0=new ArrayList<>();
                    list0.forEach(js->{
                        AchievementItem achievementItem=JSON.parseObject(js,AchievementItem.class);
                        itemList0.add(achievementItem);
                    });
                    allAchievementItemAllList.setZeroAchievementList(itemList0);
                    break;
                case 1:
                    List<String> list1=JSON.parseArray(list.get(1).getContent(),String.class);
                    List<AchievementItem> itemList1=new ArrayList<>();
                    list1.forEach(js->{
                        AchievementItem achievementItem=JSON.parseObject(js,AchievementItem.class);
                        itemList1.add(achievementItem);
                    });
                    allAchievementItemAllList.setOneAchievementList(itemList1);
                    break;
                case 2:
                    List<String> list2=JSON.parseArray(list.get(2).getContent(),String.class);
                    List<AchievementItem> itemList2=new ArrayList<>();
                    list2.forEach(js->{
                        AchievementItem achievementItem=JSON.parseObject(js,AchievementItem.class);
                        itemList2.add(achievementItem);
                    });
                    allAchievementItemAllList.setTwoAchievementList(itemList2);
                    break;
                case 3:
                    List<String> list3=JSON.parseArray(list.get(3).getContent(),String.class);
                    List<AchievementItem> itemList3=new ArrayList<>();
                    list3.forEach(js->{
                        AchievementItem achievementItem=JSON.parseObject(js,AchievementItem.class);
                        itemList3.add(achievementItem);
                    });
                    allAchievementItemAllList.setThreeAchievementList(itemList3);
                    break;
                case 4:
                    List<String> list4=JSON.parseArray(list.get(4).getContent(),String.class);
                    List<AchievementItem> itemList4=new ArrayList<>();
                    list4.forEach(js->{
                        AchievementItem achievementItem=JSON.parseObject(js,AchievementItem.class);
                        itemList4.add(achievementItem);
                    });
                    allAchievementItemAllList.setFourAchievementList(itemList4);
                    break;
                case 5:
                    List<String> list5=JSON.parseArray(list.get(5).getContent(),String.class);
                    List<AchievementItem> itemList5=new ArrayList<>();
                    list5.forEach(js->{
                        AchievementItem achievementItem=JSON.parseObject(js,AchievementItem.class);
                        itemList5.add(achievementItem);
                    });
                    allAchievementItemAllList.setFiveAchievementList(itemList5);
                    break;
                case 6:
                    List<String> list6=JSON.parseArray(list.get(6).getContent(),String.class);
                    List<AchievementItem> itemList6=new ArrayList<>();
                    list6.forEach(js->{
                        AchievementItem achievementItem=JSON.parseObject(js,AchievementItem.class);
                        itemList6.add(achievementItem);
                    });
                    allAchievementItemAllList.setSixAchievementList(itemList6);
                    break;
                case 7:
                    List<String> list7=JSON.parseArray(list.get(7).getContent(),String.class);
                    List<AchievementItem> itemList7=new ArrayList<>();
                    list7.forEach(js->{
                        AchievementItem achievementItem=JSON.parseObject(js,AchievementItem.class);
                        itemList7.add(achievementItem);
                    });
                    allAchievementItemAllList.setSevenAchievementList(itemList7);
                    break;
                case 8:
                    List<String> list8=JSON.parseArray(list.get(8).getContent(),String.class);
                    List<AchievementItem> itemList8=new ArrayList<>();
                    list8.forEach(js->{
                        AchievementItem achievementItem=JSON.parseObject(js,AchievementItem.class);
                        itemList8.add(achievementItem);
                    });
                    allAchievementItemAllList.setEightAchievementList(itemList8);
                    break;
            }
        }
        return allAchievementItemAllList;
    }

    public String selectAllAchievement(String jsessionid,String type){

        Map<String,String> map=new HashMap<>();
        map.put("page","1");
        map.put("rows","150");
        map.put("sort","xnxqdm,kcbh,ksxzdm");
        map.put("order","asc");
        map.put("jhlxdm",type);//01培养方案  02公选课

        String result=HttpUnits.gyPost("https://jwxt.gdupt.edu.cn/xskccjxx!getDataList.action",jsessionid,map);

        return result;
    }

}
